﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using YouHP.Model;
using YouHP.BLL;
using YouHP.Utility;

namespace web.admin
{
    public partial class LabcheckEdit : System.Web.UI.Page
    {
        private string URL_BACK = "Labcheck.aspx";
        private LabcheckBLL labcheckBll = LabcheckBLL.Instance;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        private void Bind()
        {
            try
            {
                IList list = new ArrayList();
                int id = RequestTools.GetQueryInt("id", -1);
                if (id > 0)
                {
                    LabcheckInfo info = labcheckBll.GetLabcheckInfo(id);
                    if (info != null)
                    {
                        if (this.ViewState["isQuantified"] == null || this.ViewState["isQuantified"].ToString().Length <= 0)
                        {
                            this.ViewState["isQuantified"] = "" + info.IsQuantified;
                        }

                        if (info.IsQuantified > 0)
                        {
                            //解析正常值字符串
                            string[] normalValues = info.NormalValues.Split('@');
                            if (normalValues.Length >= 2)
                            {
                                info.NormalMinValue = Convert.ToDouble(normalValues[0]);
                                info.NormalMaxValue = Convert.ToDouble(normalValues[1]);
                            }

                            //解析上下限字符串
                            string[] limitValues = info.LimitValues.Split('@');
                            if (limitValues.Length >= 2)
                            {
                                info.MinValue = Convert.ToDouble(limitValues[0]);
                                info.MaxValue = Convert.ToDouble(limitValues[1]);
                            }
                        }
                        
                        list.Add(info);
                    }
                }

                this.labcheckInfo.DataSource = list;
                this.labcheckInfo.DataBind();
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        protected void DataBound(object sender, EventArgs e)
        {
            try
            {
                ((RadioButtonList)this.labcheckInfo.FindControl("isQuantified")).SelectedValue = this.ViewState["isQuantified"].ToString();
                if (this.ViewState["isQuantified"].ToString().Equals("1"))
                {
                    this.labcheckInfo.Fields[2].Visible = true;
                    this.labcheckInfo.Fields[3].Visible = true;
                    this.labcheckInfo.Fields[4].Visible = true;
                    this.labcheckInfo.Fields[5].Visible = false;
                }
                else
                {
                    this.labcheckInfo.Fields[2].Visible = false;
                    this.labcheckInfo.Fields[3].Visible = false;
                    this.labcheckInfo.Fields[4].Visible = false;
                    this.labcheckInfo.Fields[5].Visible = true;
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        protected void UpdateButton_click(object sender, EventArgs e)
        {
            //数据有效性检查
            try
            {
                LabcheckInfo info = validateForm(this.labcheckInfo);
                if (info != null)
                {
                    info.Id = Convert.ToInt32(this.labcheckInfo.DataKey.Value);

                    //准备数据,调用SqlServerDAL执行更新
                    if (labcheckBll.UpdateLabcheck(info))  //成功
                    {
                        HttpContext.Current.Response.Redirect(URL_BACK, true);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void CancelButton_click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Redirect(URL_BACK, true);
        }

        protected void IsQuantifiedChanged(object sender, EventArgs e)
        {
            if (((RadioButtonList)this.labcheckInfo.FindControl("isQuantified")).SelectedValue.Equals("1"))
            {
                this.labcheckInfo.Fields[2].Visible = true;
                this.labcheckInfo.Fields[3].Visible = true;
                this.labcheckInfo.Fields[4].Visible = true;
                this.labcheckInfo.Fields[5].Visible = false;
            }
            else
            {
                this.labcheckInfo.Fields[2].Visible = false;
                this.labcheckInfo.Fields[3].Visible = false;
                this.labcheckInfo.Fields[4].Visible = false;
                this.labcheckInfo.Fields[5].Visible = true;
            }

            this.ViewState["isQuantified"] = ((RadioButtonList)this.labcheckInfo.FindControl("isQuantified")).SelectedValue;
        }

        //数据有效性检查
        private LabcheckInfo validateForm(DetailsView formViewCtl)
        {
            LabcheckInfo info = new LabcheckInfo();

            info.Name = ((TextBox)formViewCtl.FindControl("name")).Text;
            info.Intro = ((TextBox)formViewCtl.FindControl("introduction")).Text;
            info.Desp = ((TextBox)formViewCtl.FindControl("description")).Text;
            info.IsQuantified = Convert.ToInt32(((RadioButtonList)formViewCtl.FindControl("isQuantified")).SelectedValue);
            info.GridValues = ((TextBox)formViewCtl.FindControl("gridValues")).Text.Trim();
            info.ShortName = ((TextBox)formViewCtl.FindControl("shortName")).Text;
            info.UpdateTime = DateTime.Now;

            if (info.IsQuantified > 0)
            {
                info.MeasurementUnits = ((TextBox)formViewCtl.FindControl("measurementUtils")).Text;
                info.MinValue = Convert.ToDouble(((TextBox)formViewCtl.FindControl("minValue")).Text);
                info.MaxValue = Convert.ToDouble(((TextBox)formViewCtl.FindControl("maxValue")).Text);
                info.NormalMinValue = Convert.ToDouble(((TextBox)formViewCtl.FindControl("normalMin")).Text);
                info.NormalMaxValue = Convert.ToDouble(((TextBox)formViewCtl.FindControl("normalMax")).Text);
                info.LimitValues = info.MinValue + "@" + info.MaxValue;
                info.NormalValues = info.NormalMinValue + "@" + info.NormalMaxValue;
            }
            else
            {
                info.ChoicesDescription = ((TextBox)formViewCtl.FindControl("choicesDescription")).Text;
            }

            return info;
        }
    }
}
